package org.coonect;

import java.awt.BorderLayout;
import java.awt.EventQueue;


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JTable;
import javax.swing.border.LineBorder;
import java.awt.Color;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.Vector;

@SuppressWarnings("serial")
public class Results extends JFrame {

	private JPanel contentPane;
	private JTable table;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Results frame = new Results();
					frame.setVisible(true);
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		
		
		
	}

	
	
	
	/**
	 * Create the frame.
	 */
	public Results() {
	
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 948, 718);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);

		table = new JTable();
		table.setBorder(new LineBorder(new Color(0, 0, 0)));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(gl_contentPane.createParallelGroup(
				Alignment.LEADING).addGroup(
				gl_contentPane.createSequentialGroup().addContainerGap()
						.addComponent(table, GroupLayout.DEFAULT_SIZE, 639,
								Short.MAX_VALUE).addContainerGap()));
		gl_contentPane
				.setVerticalGroup(gl_contentPane.createParallelGroup(
						Alignment.LEADING).addGroup(
						gl_contentPane.createSequentialGroup().addGap(13)
								.addComponent(table, GroupLayout.DEFAULT_SIZE,
										587, Short.MAX_VALUE)));
		contentPane.setLayout(gl_contentPane);
	}

	@SuppressWarnings("unchecked")
	public void Getdata() {
		Vector columnNames = new Vector();
		Vector data = new Vector();

		try {
			// Connect to an Access Database

			// String driver = "sun.jdbc.mysql.JdbcMysqlDriver";
			// String url = "jdbc:odbc:???"; // if using ODBC Data Source name
			String url = "jdbc:h2:~/test?useEncoding=true&characterEncoding=UTF-8";
			String userid = "sa";
			String password = "";

			// Class.forName( driver );
			Connection connection = DriverManager.getConnection(url, userid,
					password);

			// Read data from a table

			String sql = "SELECT * FROM DATABASE1";
			Statement stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			ResultSetMetaData md = rs.getMetaData();
			int columns = md.getColumnCount();

			// Get column names

			for (int i = 1; i <= columns; i++) {
				columnNames.addElement(md.getColumnName(i));
			}

			// Get row data

			while (rs.next()) {
				Vector row = new Vector(columns);

				for (int i = 1; i <= columns; i++) {
					row.addElement(rs.getObject(i));

				}

				data.addElement(row);
			}

			rs.close();
			stmt.close();
			connection.close();
		} catch (Exception e) {
			System.out.println(e);
		}

		// Create table with database data

		JTable table = new JTable(data, columnNames) {

			public Class getColumnClass(int column) {
				for (int row = 0; row < getRowCount(); row++) {
					Object o = getValueAt(row, column);

					if (o != null) {
						return o.getClass();

					}
				}

				return Object.class;
			}
		};

		JScrollPane scrollPane = new JScrollPane(table);
		getContentPane().add(scrollPane);

		JPanel buttonPanel = new JPanel();
		getContentPane().add(buttonPanel, BorderLayout.SOUTH);

		
	}

}
